﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Common;
using System.Configuration;
using WebApplication.DTO;
using WebApplication.DAO;

namespace WebApplication
{
    public partial class Default : System.Web.UI.Page
    {
        List<keyDTO> lk = new List<keyDTO>();
        List<csdlDTO> CSDL = new List<csdlDTO>();
        List<dichvuDTO> tableDichVu = new List<dichvuDTO>();
        List<diadiemDTO> tableDiaDiem = new List<diadiemDTO>();
        List<duongDTO> tableDuong = new List<duongDTO>();
        List<phuongDTO> tablePhuong = new List<phuongDTO>();
        List<quanhuyenDTO> tableQuanHuyen = new List<quanhuyenDTO>();
        List<thanhphoDTO> tableThanhPho = new List<thanhphoDTO>();
        List<tukhoaDTO> tableTuKhoaDiaDiem = new List<tukhoaDTO>();
        List<tukhoaDTO> tableTuKhoaDichVu = new List<tukhoaDTO>();
        List<tukhoaDTO> tableTuKhoaDuong = new List<tukhoaDTO>();
        List<tukhoaDTO> tableTuKhoaPhuong = new List<tukhoaDTO>();
        List<tukhoaDTO> tableTuKhoaQuanHuyen = new List<tukhoaDTO>();
        List<tukhoaDTO> tableTuKhoaTinhThanh = new List<tukhoaDTO>();

        int flagKQ = 0;
        int[] arrKQ = {0,0,0,0,0,0};
        String key;
        DbConnection dbconnect;

        protected void Page_Load(object sender, EventArgs e)
        {
            
               
                    string strconnect = "";

                    //lay chuoi ket noi tu file config
                    string provider = ConfigurationManager.ConnectionStrings["providerName"].ConnectionString;
                    strconnect = ConfigurationManager.ConnectionStrings["server"].ConnectionString;//.Replace(";","") + ";";
                    strconnect += ConfigurationManager.ConnectionStrings["security"].ConnectionString;

                    string database = ConfigurationManager.ConnectionStrings["database"].ConnectionString;
                    int daubangcuoi = database.LastIndexOf("=");
                    string filedatabase = database.Substring(daubangcuoi + 1, database.Length - daubangcuoi - 2);
                    filedatabase = filedatabase.Trim();//xoa khoang trang dau va cuoi neu co cua ten file


                    string strDatabasePath = Server.MapPath("App_Data\\" + filedatabase);//lay thu muc hien hanh cua porejct va cong them chuoi "App_Data\\"+ filedatabase
                    strconnect += "Data Source=" + strDatabasePath;//day la chuoi ket noi day du

                    //sử dụng provider  
                    DbProviderFactory dbprovider = DbProviderFactories.GetFactory(provider);
                    //strconnect += strconnect + strDatabasePath;
                    //kết nối đến hệ quản trị
                    dbconnect = dbprovider.CreateConnection();
                    dbconnect.ConnectionString = strconnect;
                    dbconnect.Open();
                 if (Session["csdlSession"] == null)
                 {
                    // CSDL = WebApplication.DAO.csdlDAO.layCSDL(dbconnect);
                    tableDichVu = WebApplication.DAO.csdlDAO.layTable_DichVu(dbconnect);
                    Session["tableDichVuSession"] = tableDichVu;
                    tableDiaDiem = WebApplication.DAO.csdlDAO.layTable_DiaDiem(dbconnect);
                    Session["tableDiaDiemSession"] = tableDiaDiem;
                    tableDuong = WebApplication.DAO.csdlDAO.layTable_Duong(dbconnect);
                    Session["tableDuongSession"] = tableDuong;
                    tablePhuong = WebApplication.DAO.csdlDAO.layTable_Phuong(dbconnect);
                    Session["tablePhuongSession"] = tablePhuong;
                    tableQuanHuyen = WebApplication.DAO.csdlDAO.layTable_QuanHuyen(dbconnect);
                    Session["tableQuanHuyenSession"] = tableQuanHuyen;
                    tableThanhPho = WebApplication.DAO.csdlDAO.layTable_ThanhPho(dbconnect);
                    Session["tableThanhPhoSession"] = tableThanhPho;

                    tableTuKhoaDichVu = WebApplication.DAO.csdlDAO.layTable_TuKhoaDichVu(dbconnect);
                    Session["tableTuKhoaDichVuSession"] = tableTuKhoaDichVu;
                    tableTuKhoaDiaDiem = WebApplication.DAO.csdlDAO.layTable_TuKhoaDiaDiem(dbconnect);
                    Session["tableTuKhoaDiemDiemSession"] = tableTuKhoaDiaDiem;
                    tableTuKhoaDuong = WebApplication.DAO.csdlDAO.layTable_TuKhoaDuong(dbconnect);
                    Session["tableTuKhoaDuongSession"] = tableTuKhoaDuong;
                    tableTuKhoaPhuong = WebApplication.DAO.csdlDAO.layTable_TuKhoaPhuong(dbconnect);
                    Session["tableTuKhoaPhuongSession"] = tableTuKhoaPhuong;
                    tableTuKhoaQuanHuyen = WebApplication.DAO.csdlDAO.layTable_TuKhoaQuanHuyen(dbconnect);
                    Session["tableTuKhoaQuanHuyenSession"] = tableTuKhoaQuanHuyen;
                    tableTuKhoaTinhThanh = WebApplication.DAO.csdlDAO.layTable_TuKhoaTinhThanh(dbconnect);
                    Session["tableTuKhoaTinhThanhSession"] = tableTuKhoaTinhThanh;




                }
                else
                {
                    //CSDL = (List<csdlDTO>)Session["csdlSession"];
                    tableDichVu = (List<dichvuDTO>)Session["tableDichVuSession"];
                    tableDiaDiem = (List<diadiemDTO>)Session["tableDiaDiemSession"];
                    tablePhuong = (List<phuongDTO>)Session["tablePhuongSession"];
                    tableQuanHuyen = (List<quanhuyenDTO>)Session["tableQuanHuyenSession"];
                    tableThanhPho = (List<thanhphoDTO>)Session["tableThanhPhoSession"];

                    tableTuKhoaDichVu = (List<tukhoaDTO>)Session["tableTuKhoaDichVuSession"];
                    tableTuKhoaDiaDiem = (List<tukhoaDTO>)Session["tableTuKhoaDiemDiemSession"];
                    tableTuKhoaDuong = (List<tukhoaDTO>)Session["tableTuKhoaDuongSession"];
                    tableTuKhoaPhuong = (List<tukhoaDTO>)Session["tableTuKhoaPhuongSession"];
                    tableTuKhoaQuanHuyen = (List<tukhoaDTO>)Session["tableTuKhoaQuanHuyenSession"];
                    tableTuKhoaTinhThanh = (List<tukhoaDTO>)Session["tableTuKhoaTinhThanhSession"];

                }
                if (Request.QueryString["page"] != null)
                {
                    int soLuong = 0;
                    int page = int.Parse(Request.QueryString["page"]);
                    
                    this.txtKey.Text = Convert.ToString(Session["textBoxSession"]);
                    String str = Convert.ToString(Session["arrKQ"]);
                    String[] arrayKQ = str.Split(' ');
                    for (int i = 0; i < arrayKQ.Count(); i++)
                    {
                        arrKQ[i] = int.Parse(arrayKQ[i]);
                    }

                    flagKQ = (int)Session["flagKQ"];
                    CSDL = WebApplication.DAO.csdlDAO.ketquaSearch(dbconnect, arrKQ, flagKQ,page, ref soLuong);
                    idSoLuongKQ.Text = "";
                    lbResult.Text = "";
                    if (CSDL != null)
                    {
                        idSoLuongKQ.Text = "(" + soLuong + " Kết Quả)";
                        lbResult.Text = csdlDAO.xuatKetQuaTimDuoc(CSDL);
                        if (soLuong > 10)
                        {
                            int soTrang = soLuong / 10;
                            if (soTrang % 10 != 0)
                            {
                                soTrang++;
                            }
                            lbTrang.Text = searchDAO.phanTrang(soTrang, page,soLuong);
                        }

                    }
                    else
                    {
                        idSoLuongKQ.Text = "(Không Tìm Thấy Kết Quả)";
                    }

                    dbconnect.Close();
                    
                }
             
        }

        protected void btSearch_Click(object sender, EventArgs e)
        {
            idSoLuongKQ.Text = "";
            lbResult.Text = "";
            lbTrang.Text = "";
            int page;
            
            if (Request.QueryString["page"] == null)
            {
                page = 1;
            }
            else
                page = int.Parse(Request.QueryString["page"]);
           
            //String url = Request.QueryString["sotrang"];
            String textBox = this.txtKey.Text;
            if (textBox != null && textBox != "")
            {
                //Session["textBoxSession"] = textBox;
                textBox = textboxDAO.timTuKhoa(textBox, tableTuKhoaTinhThanh,arrKQ,5,ref flagKQ).Trim();
                textBox = textboxDAO.timTuKhoa(textBox, tableTuKhoaQuanHuyen, arrKQ, 4,ref flagKQ).Trim();
                textBox = textboxDAO.timTuKhoa(textBox, tableTuKhoaPhuong, arrKQ, 3,ref flagKQ).Trim();
                textBox = textboxDAO.timTuKhoa(textBox, tableTuKhoaDichVu, arrKQ, 0,ref flagKQ).Trim();
                textBox = textboxDAO.timTuKhoa(textBox, tableTuKhoaDuong, arrKQ, 2,ref flagKQ).Trim();
                textBox = textboxDAO.timTuKhoa(textBox, tableTuKhoaDiaDiem, arrKQ, 1,ref flagKQ).Trim();
                this.txtKey.Text = textBox;
                String textChuanHoa = textboxDAO.chuanHoaChuoi(textBox);
                textChuanHoa = textboxDAO.xoaKyTuDacBiet(textChuanHoa);
                textChuanHoa = textChuanHoa.ToUpper();
                //String kq = textBoxDAO.xoaDau(text);

                if(arrKQ[0]==0)
                for (int i = 0; i < tableDichVu.Count; i++)
                {
                    if (textChuanHoa.Contains(tableDichVu[i].TenDichVu))
                    {
                        arrKQ[0] = tableDichVu[i].MaDichVu;
                        textChuanHoa = textChuanHoa.Replace(tableDichVu[i].TenDichVu, "");
                        textChuanHoa = textboxDAO.chuanHoaChuoi(textChuanHoa);
                        flagKQ = 1;
                        break;
                    }
                }

                if (arrKQ[1] == 0)
                for (int i = 0; i < tableDiaDiem.Count; i++)
                {
                    if (textChuanHoa.Contains(tableDiaDiem[i].TenDiaDiem))
                    {
                        arrKQ[1] = tableDiaDiem[i].MaDiaDiem;
                        textChuanHoa = textChuanHoa.Replace(tableDiaDiem[i].TenDiaDiem, "");
                        textChuanHoa = textboxDAO.chuanHoaChuoi(textChuanHoa);
                        flagKQ = 1;
                        break;
                    }
                }

                if (arrKQ[2] == 0)
                for (int i = 0; i < tableDuong.Count; i++)
                {
                    if (textChuanHoa.Contains(tableDuong[i].TenDuong))
                    {
                        arrKQ[2] = tableDuong[i].MaDuong;
                        textChuanHoa = textChuanHoa.Replace(tableDuong[i].TenDuong, "");
                        textChuanHoa = textboxDAO.chuanHoaChuoi(textChuanHoa);
                        flagKQ = 1;
                        break;
                    }
                }

                if (arrKQ[3] == 0)
                for (int i = 0; i < tablePhuong.Count; i++)
                {
                    if (textChuanHoa.Contains(tablePhuong[i].TenPhuong))
                    {
                        arrKQ[3] = tablePhuong[i].MaPhuong;
                        textChuanHoa = textChuanHoa.Replace(tablePhuong[i].TenPhuong, "");
                        textChuanHoa = textboxDAO.chuanHoaChuoi(textChuanHoa);
                        flagKQ = 1;
                        break;
                    }
                }

                if (arrKQ[4] == 0)
                for (int i = 0; i < tableQuanHuyen.Count; i++)
                {
                    if (textChuanHoa.Contains(tableQuanHuyen[i].TenQuanHuyen))
                    {
                        arrKQ[4] = tableQuanHuyen[i].MaQuanHuyen;
                        textChuanHoa = textChuanHoa.Replace(tableQuanHuyen[i].TenQuanHuyen, "");
                        textChuanHoa = textboxDAO.chuanHoaChuoi(textChuanHoa);
                        flagKQ = 1;
                        break;
                    }
                }

                if (arrKQ[5] == 0)
                for (int i = 0; i < tableThanhPho.Count; i++)
                {
                    if (textChuanHoa.Contains(tableThanhPho[i].TenThanhPho))
                    {
                        arrKQ[5] = tableThanhPho[i].MaThanhPho;
                        textChuanHoa = textChuanHoa.Replace("THÀNH PHỐ " + tableThanhPho[i].TenThanhPho, "");
                        textChuanHoa = textChuanHoa.Replace(tableThanhPho[i].TenThanhPho, "");

                        textChuanHoa = textboxDAO.chuanHoaChuoi(textChuanHoa);
                        flagKQ = 1;
                        break;
                    }
                }
                
                    int soLuong = 0;
                    CSDL = WebApplication.DAO.csdlDAO.ketquaSearch(dbconnect, arrKQ, flagKQ,page, ref soLuong);
                    idSoLuongKQ.Text = "";
                    lbResult.Text = "";
                    if (CSDL != null)
                    {
                        idSoLuongKQ.Text = "(" + soLuong + " Kết Quả)";
                        lbResult.Text = csdlDAO.xuatKetQuaTimDuoc(CSDL);
                        if (soLuong > 10)
                        {
                            int soTrang = soLuong / 10;
                            if (soTrang % 10 != 0)
                            {
                                soTrang++;
                            }
                            lbTrang.Text = searchDAO.phanTrang(soTrang, page,soLuong);
                        }

                    }
                    else
                    {
                        idSoLuongKQ.Text = "(Không Tìm Thấy Kết Quả)";
                    }

                    Session["arrKQ"] = keyDAO.gepKey(arrKQ);
                    Session["flagKQ"] = flagKQ;
                    Session["csdlSession"] = CSDL;
                    
                    dbconnect.Close();

                
               
        
            }
        }

      
    }
}